//
//  OrderAgainCommentViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/12.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "OrderAgainCommentViewController.h"
#import "MeatComplationSuccessViewController.h"

#import "MeatComplaintCustomCell.h"
#import "MeatComplaintIphoneCell.h"

#import "MeatComplaintModel.h"
#import "OrderCommentCell.h"
#import "OrderStarCell.h"


@interface OrderAgainCommentViewController()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) MeatComplaintModel *model;

@end


@implementation OrderAgainCommentViewController

- (void)setupUI
{
    self.baseNavView.navTitleLabel.text = @"追加评价";
    self.baseNavView.line.hidden = YES;
    [self.baseNavView.rightNavBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.baseNavView.rightNavBtn.hidden = NO;
    @weakify(self);
    self.baseNavView.rightBlock = ^{
        @strongify(self);
        [self showHudWithMsg:@"提交"];
    };
}

- (void)setupLayout
{
    self.reloadBtn.mj_y -= 100;
    self.reloadLab.mj_y -= 100;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(TJ_NavBarHeight, 0, 0, 0));
    }];
}

- (void)initData {
    
}


#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }else if (indexPath.section == 1){
        return 34;
    }
    return 251;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        OrderCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCommentCell"];
        return cell;
        
    }else if (indexPath.section == 1){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell_sectionLittle"];
        
        cell.textLabel.text = @"追加一下您的评论吧";
        cell.textLabel.textColor = UIColorFromRGB(0x969696);
        cell.textLabel.font = mFont(14);
        cell.backgroundColor = mBackgroudColor;
        
        return cell;
    }
    
    MeatComplaintCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ComplaintBusinessCustomCell"];
    cell.model = self.model;
    
    kWeakSelf(self);
    cell.dealPhotoBlock = ^(NSInteger index, DealPhotoOperation operation) {
        
        if (operation == DealPhotoOperation_Add) {
            [weakself addPhotoOperationByTableView:tableView];
        }else{
            [weakself.model.photos removeObjectAtIndex:index];
            [weakself localReloadTableViewBySection:2];
        }
        [weakself checkUpdateDataIsWhole];
    };
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        self.model.currentIndex = indexPath.row;
        self.model.content = self.model.questions[indexPath.row];
        [self localReloadTableViewBySection:0];
        [self checkUpdateDataIsWhole];
    }
}


#pragma mark - UIImagePicker Delegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    if (!image) {
        NSLog(@"获取头像照片失败");
    }else {
        [self.model.photos addObject:image];
        [self localReloadTableViewBySection:2];
        [self checkUpdateDataIsWhole];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - target action

//跳转相册
- (void)addPhotoOperationByTableView:(UITableView *)tableView{
    //修改头像
    [tableView endEditing:YES];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    kWeakSelf(self)
    UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself presentViewController:weakself.imagePicker animated:YES completion:nil];
    }];
    UIAlertAction * photosAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.allowsEditing = YES;
        [weakself presentViewController:picker animated:YES completion:nil];
    }];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cameraAction];
    [alert addAction:photosAction];
    [alert addAction:cancelAction];
    [weakself presentViewController:alert animated:YES completion:nil];
}

//检测数据
- (void)checkUpdateDataIsWhole{
    if (
        ![self isBlankString:self.model.content]
        &&![self isBlankString:self.model.phone]
        )
    {
        self.model.isSumbit = YES;
    }else{
        self.model.isSumbit = NO;
    }
}

#pragma mark - setter and getter

- (void)registerTableViewCell{
    
    self.tableView.separatorColor = KClearColor;
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_header.hidden = YES;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell_sectionLittle"];
    [self.tableView registerClass:[MeatComplaintCustomCell class] forCellReuseIdentifier:@"ComplaintBusinessCustomCell"];
    [self.tableView registerClass:[OrderCommentCell class] forCellReuseIdentifier:@"OrderCommentCell"];
    
}

- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        _imagePicker.allowsEditing = YES;
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

- (MeatComplaintModel *)model{
    if (!_model) {
        _model = [[MeatComplaintModel alloc] init];
    }
    return _model;
}


#pragma mark - other methods

- (void)textViewDidChanged:(NSNotification *)notification
{
    UITextView *textView = notification.object;
    self.model.content = textView.text;
}

- (void)textFieldDidChanged:(NSNotification *)notification
{
    UITextField *textField = notification.object;
    self.model.phone = textField.text;
}

- (void)keyboardDidShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    kWeakSelf(self);
    [UIView animateWithDuration:duration animations:^{
        weakself.tableView.mj_y = 0;
    }];
    
    //当键盘升起的时候添加点击降落操作
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    }
    [self.view addGestureRecognizer:self.tap];
}

//取消键盘响应
- (void)hideKeyboard{
    
    [self.view endEditing:YES];
}

- (void)keyboardWillHidden:(NSNotification *)notification{
    
    self.tableView.mj_y = 0;
    if (self.tap) {
        [self.view removeGestureRecognizer:self.tap];
    }
}

- (void)addNotifications{
    
    [kNotificationCenter addObserver:self selector:@selector(textViewDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(textFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeNotifications{
    
    [kNotificationCenter removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    [kNotificationCenter removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    [kNotificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)dealloc
{
    [self removeNotifications];
}

@end
